with open('word2idx.pk', 'rb') as f:
    word2idx = pickle.load(f)
with open('idx2word.pk', 'rb') as f:
    idx2word = pickle.load(f)
with open('label2idx.pk', 'rb') as f:
    label2idx = pickle.load(f)
with open('idx2label.pk', 'rb') as f:
    idx2label = pickle.load(f)
# 训练数据中所有词的个数
vocab_size = len(word2idx.keys())  # 词汇表大小
# 标签类别,分别为法治、健康等
label_size = len(label2idx.keys())  # 标签类别数量
df = pd.read_csv(file_path, encoding='utf-8', sep='\t') 
# 序列填充,按input_shape填充,长度不足的按0补充
# 将一句话映射成对应的索引 [0,24,63...]
x = [torch.tensor([(word2idx[word]) for word in sent]) for sent in df['text']] 
# 如果长度不够input_shape,使用0进行填充
import torch.nn.functional as F
x = [v[0:input_shape] 
if len(v)>input_shape else F.pad(v,(0,input_shape-len(v)), "constant",0) for v in x]
# 形成标签0和1
y = [[label2idx[sent]] for sent in df['label']]
y = np.array(y)  
